package com.jg.ads.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.jg.ads.R;
import com.jg.ads.utils.MyDisplayUtil;
import com.jg.ads.utils.SPUtil;

public class SwitchView extends RelativeLayout {
    private String title;

    private Switch sw;

    private OnSwitchChangeListener listener;

    public SwitchView(Context context) {
        super(context);
        initView();
    }

    public SwitchView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initAttr(attrs);
        initView();
    }

    public SwitchView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initAttr(attrs);
        initView();
    }

    private void initAttr(AttributeSet attrs) {
        if (attrs == null) {
            return;
        }
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.SwitchView);
        title = typedArray.getString(R.styleable.SwitchView_text);
        typedArray.recycle();
    }

    private void initView() {

        setBackgroundColor(Color.WHITE);

        TextView tv = new TextView(getContext());

        tv.setText(TextUtils.isEmpty(title) ? "标题" : title);

        LayoutParams lpTv = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        lpTv.addRule(ALIGN_PARENT_LEFT);
        lpTv.addRule(CENTER_VERTICAL);
        lpTv.leftMargin = MyDisplayUtil.dp2px(15);

        addView(tv, lpTv);

        sw = new Switch(getContext());

        LayoutParams lpSw = new LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT
        );
        lpSw.addRule(ALIGN_PARENT_RIGHT);
        lpSw.addRule(CENTER_VERTICAL);

        addView(sw, lpSw);
    }

    public void setListener(String key) {
        setListener(key, null);
    }

    public void setListener(final String key, final OnSwitchChangeListener listener) {
        sw.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                SPUtil.putBoolean(getContext(), key, isChecked);
                if (listener != null) {
                    listener.onChange(isChecked);
                }
            }
        });
    }

    public void setSwitchState(String key, boolean defaultState) {
        boolean state = SPUtil.getBoolean(getContext(), key, defaultState);
        sw.setChecked(state);
    }

    public boolean getSwitchState(String key, boolean defaultState) {
        boolean state = SPUtil.getBoolean(getContext(), key, defaultState);
        return state;
    }

    public interface OnSwitchChangeListener {
        void onChange(boolean isChecked);
    }

}
